package com.mskj.zxmy.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.mskj.zxmy.common.Result;
import com.mskj.zxmy.dto.Material;
import com.mskj.zxmy.dto.PageResult;
import com.mskj.zxmy.dto.ProductSelectFo;
import com.mskj.zxmy.dto.ReportInfo;
import com.mskj.zxmy.dto.TaskDto;
import com.mskj.zxmy.entity.CollarList;
import com.mskj.zxmy.entity.RepositoryApplication;
import com.mskj.zxmy.mapper.MyTaskMapper;
import com.mskj.zxmy.service.MyTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: zxmy
 * @BelongsPackage: com.mskj.zxmy.controller
 * @Author: lk
 * @CreateTime: 2025-07-22
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/my-task")
@RequiredArgsConstructor
@SaIgnore
@Tag(name = "我的任务", description = "我的任务")
@Slf4j
@CrossOrigin
public class MyTaskController {

    private final MyTaskService myTaskService;
    private final MyTaskMapper myTaskMapper;

    @GetMapping("/query-task-info")
    @Operation(description = "查询查询工序任务信息", method = "GET")
    public Result<TaskDto> queryTaskInfo(@Parameter(description = "生产工单ID", required = true) Integer orderId, @Parameter(description = "工序ID", required = true) Integer processId) {
        return Result.success(myTaskService.queryTaskInfo(orderId, processId));
    }

    @GetMapping("/begin-process-task")
    @Operation(description = "开始工序任务", method = "GET")
    public Result<String> beginProcessTask(@Parameter(description = "工序任务ID", required = true) Integer processTaskId) {
        return Result.success(myTaskService.beginProcessTask(processTaskId));
    }

    @GetMapping("/report-works-info")
    @Operation(description = "报工信息", method = "GET")
    public Result<ReportInfo> reportWorksInfo(@Parameter(description = "工艺任务ID", required = true) Integer processTaskId) {
        return Result.success(myTaskService.getReportingWorks(processTaskId));
    }

    @GetMapping("/scan-get-barcode")
    @Operation(description = "扫描获取条码批次code", method = "GET")
    public Result<String> scanGetBarcode(@Parameter(description = "条码ID") Integer barcodeId) {
        return Result.data(myTaskMapper.getCodeById(barcodeId));
    }

    @PostMapping("/product-report")
    @Operation(description = "生产报工", method = "POST")
    public Result<Boolean> productReport(@RequestBody ReportInfo reportInfo) throws ParseException {
        return Result.success(myTaskService.productReport(reportInfo));
    }


    @GetMapping("/lld-back-info")
    @Operation(description = "出库申请（领料）预创建信息回显", method = "GET")
    public Result<RepositoryApplication> llBackInfo(@Parameter(description = "工序任务ID") Integer processTaskId) {
        return Result.success(myTaskService.SQCKBackInfo(processTaskId));
    }

    @PostMapping("/lld-save")
    @Operation(description = "出库申请(领料)单保存", method = "POST")
    public Result<Boolean> lldSave(@RequestBody RepositoryApplication repositoryApplication) {
        return Result.success(myTaskService.CKSQSave(repositoryApplication));
    }

    @GetMapping("/prod-select")
    @Operation(description = "物料列表分页查询", method = "GET")
    public Result<PageResult<Material>> prodSelect(@ParameterObject ProductSelectFo fo) {
        long start = System.currentTimeMillis();
        
        // 查询总数
        Long total = myTaskMapper.countProductList(fo);
        
        // 计算偏移量
        int offset = (fo.getPageNum() - 1) * fo.getPageSize();
        
        // 查询数据列表
        List<Material> productList = myTaskMapper.getProductListWithPage(fo, offset, fo.getPageSize());

        PageResult<Material> pageResult = new PageResult<>(productList, total, fo.getPageNum(), fo.getPageSize());
        
        log.info("耗时：{}ms", System.currentTimeMillis() - start);
        return Result.success(pageResult);
    }

}
